const path = require('path');
const Cli = require('webpack-cli');

const dirMap = {
  '01': 'study01-loader',
  '02': 'study02-compiler',
  '03': 'study03-externals',
  '04': 'study04-pkg/src',
};

const args = process.argv.slice(2);
const eg = args.find((e) => /^eg=/.test(e));
const no = eg ? eg.substring(3).padStart(2, '0') : undefined;
if (!dirMap[no]) {
  console.log('----- [ERROR] Build "eg value" must be one of [' + Array.from(Object.keys(dirMap)).join(', ') + ']');
  process.exit(0);
}

const ctx = path.resolve(__dirname, dirMap[no]);
const config = require(path.resolve(ctx, 'webpack.config.js'));
const cli = new Cli();
process.argv.length = 2;

// 一切似乎很完美，但是运行为什么会报错？
cli.run(undefined, config);
